package br.edu.ufcg.embedded.eolica.view;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import br.edu.ufcg.embedded.eolica.MIDlet.UsinaEolicaMIDlet;
import br.edu.ufcg.embedded.eolica.util.Properties;

public class ConsultCanvas extends Canvas implements CommandListener {

	private Displayable previous;
	private Command cmdNext, cmdBack;
	private Display d;
	private Properties prop;
	private ConsultFormAtt consultAtt;
	private String consult;
	String[] stationNames;
	int stationIndex;
	private Image im;

	public ConsultCanvas(Display d, Displayable previous, String consult) {
		this.d = d;
		this.previous = previous;
		this.consult = consult;

		loadMap();
		loadComponents();
	}

	protected void paint(Graphics g) {
		g.setColor(0, 0, 0);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(0, 0, 0);
	//	g.setFont(Font.getDefaultFont());
		g.drawImage(im, 0, 0, Graphics.TOP | Graphics.LEFT);
	//	g.drawString("Eletrobras - " + consult, getWidth() / 2, 0, Graphics.TOP
	//			| Graphics.HCENTER);
		g.setColor(255, 0, 0);
		if (stationIndex == 0) {
			g.fillRect(97, 84, 5, 5);
		}
		if (stationIndex == 1) {
			g.fillRect(152, 68, 5, 5);
		}
		if (stationIndex == 2) {
			g.fillRect(203, 36, 5, 5);
		}
		if (stationIndex == 3) {
			g.fillRect(186, 80, 5, 5);
		}
		g.setColor(0, 0, 0);
	}

	protected void keyPressed(int keyCode) {
		if (keyCode == -4) {
			stationIndex++;
			if (stationIndex > 3)
				stationIndex = 0;
		}
		if (keyCode == -3) {
			stationIndex--;
			if (stationIndex < 0) {
				stationIndex = 3;
			}
		}
		repaint();
	}

	private void loadComponents() {
		prop = UsinaEolicaMIDlet.getProp();
		cmdNext = new Command(prop.getProperty("label.next"), Command.OK, 0);
		cmdBack = new Command(prop.getProperty("label.back"), Command.BACK, 0);

		stationNames = new String[] { "tac1", "tac2", "tac3", "tac4" };
		stationIndex = 0;

		addCommand(cmdNext);
		addCommand(cmdBack);
		setCommandListener(this);
	}

	private void loadMap() {
		//InputStream input = getClass().getResourceAsStream("/mapa-paraiba.png");
		InputStream input = getClass().getResourceAsStream("/mapa-paraiba-last-c1.png");
		byte[] imagebyte;
		try {
			imagebyte = new byte[input.available()];
			input.read(imagebyte);
			im = Image.createImage(imagebyte, 0, imagebyte.length);
		} catch (IOException e) {
			d.setCurrent(new ErrorAlert(prop.getProperty("label.error"), prop
					.getProperty("error.internal")), previous);
		} catch (Exception e) {
			d.setCurrent(new ErrorAlert(prop.getProperty("label.error"), prop
					.getProperty("error.internal")), previous);
		}
	}

	public void commandAction(Command cmd, Displayable arg1) {
		if (cmd == cmdBack)
			d.setCurrent(previous);
		else if (cmd == cmdNext) {
			if (consultAtt == null)
				consultAtt = new ConsultFormAtt(d, this, prop
						.getProperty("label.graphAtt"));

			consultAtt.setSelectedStation(stationNames[stationIndex]);
			// consultAtt.setGraphType(graphType);
			d.setCurrent(consultAtt);
		}
	}

}
